//
//  PopMenu.swift
//  TumblrPopMenu
//
//  Created by DW on 16/5/29.
//  Copyright © 2016年 lu. All rights reserved.
//

import Foundation
import UIKit


class PopMenu: UIView{

//    var photoButton: UIButton!
    var backgroundView: UIView!
    var nevermindButton: UIButton!
    var centerHigh: [CGPoint]!
    var centerLow: [CGPoint]!
    var centerMenu: [CGPoint]!
    var delegate: PopMenuDelegate?
    var isHidding: Bool = false
    

    
//    var popDetailView:trackingDetailView?
    var popDetailView:UIView?

    var isShowDetail: Bool = false
    
    init(frame: CGRect,isDetail:Bool,detailPage:UIView) {
        super.init(frame: frame)
        self.isShowDetail = isDetail
//        self.popDetailView = detailPage as! trackingDetailView
        self.popDetailView = detailPage as! UIView

        initImageView()
        initCenterArray(frame)
        setupView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    
    private func initCenterArray(frame: CGRect){
        let widthUnit = frame.width/4
        let heightHigh = frame.origin.y - 52.5
        let heightLow = frame.height + 52.5
        let gap = CGFloat(52.5 + 5)
        centerHigh = [CGPoint]()
        centerHigh.append(CGPoint(x: widthUnit, y: heightHigh))
        centerHigh.append(CGPoint(x: widthUnit*2, y: heightHigh))
        centerHigh.append(CGPoint(x: widthUnit*3, y: heightHigh))
        
        centerLow = [CGPoint]()
        centerLow.append(CGPoint(x: widthUnit, y: heightLow))
        centerLow.append(CGPoint(x: widthUnit*2, y: heightLow))
        centerLow.append(CGPoint(x: widthUnit*3, y: heightLow))
        centerLow.append(CGPoint(x: widthUnit*2, y: frame.height + nevermindButton.frame.height/2))
        
        centerMenu = [CGPoint]()
        centerMenu.append(CGPoint(x: widthUnit, y: frame.height/2 - gap))
        centerMenu.append(CGPoint(x: widthUnit*2, y: frame.height/2 - gap))
        centerMenu.append(CGPoint(x: widthUnit*3, y: frame.height/2 - gap))
        centerMenu.append(CGPoint(x: widthUnit, y: frame.height/2 + gap))
        centerMenu.append(CGPoint(x: widthUnit*2, y: frame.height/2 + gap))
        centerMenu.append(CGPoint(x: widthUnit*3, y: frame.height/2 + gap))
        centerMenu.append(CGPoint(x: widthUnit*2, y: frame.height - nevermindButton.frame.height/2))
    }
    
    private func initImageView(){
        //对传过来的View进行判断
        var width:CGFloat?
        var height:CGFloat?
        
        if self.popDetailView?.isKindOfClass(forgetPassPopView) == true {
            width = UIScreen.mainScreen().bounds.width * CGFloat(0.75)
            height = width! * CGFloat(1.5)
        }else{
            width = UIScreen.mainScreen().bounds.width * CGFloat(0.75)
            height = width! * CGFloat(1.5)
        }


        nevermindButton = UIButton(type: .System)
        nevermindButton.frame = CGRectMake(0, 0, self.frame.width, 52.5)
        nevermindButton.setTitle("Cancle", forState: .Normal)
        nevermindButton.backgroundColor = UIColor.redColor()
        backgroundView = UIView(frame: self.frame)
        if(((popDetailView?.isKindOfClass(insertUserIDView)) == false)&&((popDetailView?.isKindOfClass(moneyOperationView)) == false)){
            popDetailView?.frame = CGRect(x: 0,y: 0,width: width!,height: height!)
        }
    }
    
    private func setupView(){
        self.hidden = true
        backgroundView.backgroundColor = UIColor(red: 61/255, green: 77/255, blue: 100/255, alpha: 0.95)
        backgroundView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "handleTap:"))
        backgroundView.userInteractionEnabled = true
        
        nevermindButton.hidden = true
        nevermindButton.userInteractionEnabled = true
        nevermindButton.backgroundColor = UIColor(red: 61/255, green: 77/255, blue: 97/255, alpha: 1.0)
        nevermindButton.tintColor = UIColor(red: 133/255, green: 141/255, blue: 152/255, alpha: 1.0)
        nevermindButton.addTarget(self, action: "handleTap:", forControlEvents: .TouchUpInside)
        self.addSubview(backgroundView)
        if self.isShowDetail == true {
            self.addSubview(nevermindButton)
        }
        self.addSubview(self.popDetailView!)
    }

    func clickMenu(sender: AnyObject){
        print("clickMenu")
        let index = (sender as! UIButton).tag
        hideMenuView()
        delegate?.didClickMenu(index)
    }
    
    func handleTap(sender: AnyObject){
        hideMenuView()
    }
    
    func hideMenuView(){
        delegate?.backToPage(self.popDetailView!)
        if isHidding{
            return
        }
        
        isHidding = true
        //Nevermind button
        nevermindButton.center = centerMenu[6]
        UIView.animateWithDuration(0.3, delay: 0.3, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in
            self.nevermindButton.center = self.centerLow[3]
            }) { (finished) -> Void in
                self.nevermindButton.hidden = true
                self.isHidding = false
        }
        
        UIView.animateWithDuration(0.6, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1.0, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in
            self.popDetailView?.center = self.centerLow[1]
            }) { (finished) -> Void in
                self.hidden = true
        }
    }
    
    func showMenuView(){
        self.hidden = false
        nevermindButton.center = centerLow[3]
        nevermindButton.hidden = false
        UIView.animateWithDuration(0.3, delay: 0.3, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in
            self.nevermindButton.center = self.centerMenu[6]
            }) { (finished) -> Void in
        }
        let width = UIScreen.mainScreen().bounds.width
        let height = UIScreen.mainScreen().bounds.height
        popDetailView?.center = CGPoint(x: width * 0.5, y: 500)
        UIView.animateWithDuration(0.6, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1.0, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in
            self.popDetailView?.center = CGPoint(x: width * 0.5, y: height * 0.5)
            }) { (finished) -> Void in
        }
    }
}



